package control
{
	import core.Control;
	import core.Model;
	import core.View;
	
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import utils.StringUtils;
	import global.GameState;
	import model.Robot;

	public class KeyBoardControl
	{
		private var stage:Stage;
		private var _view:View = View.getInstance();
		private var _model:Model = Model.getInstance();
		private var _control:Control = Control.getInstance();
		
		public function KeyBoardControl(stage:Stage)
		{
			this.stage = stage;
			addListeners();
		}
		
		private function addListeners():void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
		}
		
		private function onKeyDown(event:KeyboardEvent):void
		{
			switch (event.keyCode)
			{
				case Keyboard.ENTER:
					searchRoom();
					sendChatMsg();
					break;
				
			}
			
			if (event.charCode == 68)
			{
				_control.debugPanelControl.open();
			}
			else if (event.charCode == 83)
			{
				_control.debugPanelControl.clear();
				_control.debugPanelControl.output(GameState.inviteLink);
				_control.debugPanelControl.output(GameState.tutorLink);
				_control.debugPanelControl.output(GameState.shopLink);
			}
			else if (event.charCode == 82)
			{
				Robot.getInstance().isStart();
			}
			
			trace(event.charCode);
			
		}
		
		public function searchRoom():void
		{
			if (stage.focus != _control.searchControl.search.textField)
			{
				return;
			}
			_control.searchControl.searchRoom();
		}
		
		public function sendChatMsg(force:Boolean = false):void
		{
			if (stage.focus != _view.playPanel.inputText && !force)
			{
				return;
			}
			var msg:String = _view.playPanel.inputText.text;
			
			msg = StringUtils.trim(msg);
			
			if (msg.length > 0)
			{
				trace("chat msg:" + msg);
				_model.netRemote.sendChatMsg(msg);
			}
			_view.playPanel.inputText.text = "";
		}
	}
}